/**
 * Filename: OfficialListAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 16:23
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeGoodsShopDetailActivity;
import com.github.postsanf.yinian.bean.YNShop;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.List;

public class TimeGoodsShopListAdapter extends RecyclerView.Adapter<TimeGoodsShopListAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context context;
    private List<YNShop> datas;
    private View mHeaderView;

    public TimeGoodsShopListAdapter(Context context, List<YNShop> datas) {
        this.context = context;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout shop_item;                            //列表View
        SimpleDraweeView shop_item_bg;                            //组背景图
        TextView shop_name;                            //组名
        TextView shop_tittle;                            //组人数

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            shop_item = (LinearLayout) itemView
                    .findViewById(R.id.shop_item);
            shop_item_bg = (SimpleDraweeView) itemView
                    .findViewById(R.id.shop_item_bg);
            shop_name = (TextView) itemView
                    .findViewById(R.id.shop_name);
            shop_tittle = (TextView) itemView
                    .findViewById(R.id.shop_tittle);
        }

        public void bindData(final YNShop listItems, final int point) {
            if (listItems != null) {
                if (point % 2 == 0) {
                    shop_item.setBackgroundColor(context.getResources().getColor(R.color.shop_bg_gray));
                } else {
                    shop_item.setBackgroundColor(context.getResources().getColor(R.color.shop_bg_white));
                }
                final int ebGoodId = listItems.getEbGoodsID();
                shop_item_bg.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getShopItemHigh(context)));
                String paths = YNUrlHelper.getHomeImgUrl(context, listItems.getEbGoodsPic());
                Uri uri = Uri.parse(paths);
                ImageOptHelper.showFrescoHome(uri, shop_item_bg, context);
                shop_name.setText(listItems.getEbGoodsName());
                shop_tittle.setText(listItems.getEbGoodsIntroduce() + ",仅售" + listItems.getEbGoodsPrice() + "元/" + listItems.getEbGoodsUnit());
                // 点击列表
                shop_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, TimeGoodsShopDetailActivity.class).putExtra("goodsID", ebGoodId));
                        switch (ebGoodId) {
                            case 1:
                                Intent statintent1 = new Intent(ACTIONs.record);
                                statintent1.putExtra("key", "makeVoicePostcard_4");
                                statintent1.putExtra("value", 1);
                                context.sendBroadcast(statintent1);
                                break;
                            case 2:
                                Intent statintent2 = new Intent(ACTIONs.record);
                                statintent2.putExtra("key", "makeLomoCard_4");
                                statintent2.putExtra("value", 1);
                                context.sendBroadcast(statintent2);
                                break;
                            case 3:
                                Intent statintent3 = new Intent(ACTIONs.record);
                                statintent3.putExtra("key", "makePhotoPuzzle_4");
                                statintent3.putExtra("value", 1);
                                context.sendBroadcast(statintent3);
                                break;
                            case 4:
                                Intent statintent4 = new Intent(ACTIONs.record);
                                statintent4.putExtra("key", "makeDrawingPin_4");
                                statintent4.putExtra("value", 1);
                                context.sendBroadcast(statintent4);
                                break;
                        }
                    }
                });
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private YNShop getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_shop, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        YNShop listItems = getItem(pos);

        holder.bindData(listItems, pos);
        holder.itemView.setTag(listItems);
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }


    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }
}
